package com.example.hospitalsystemclient.ui.CheckList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hospitalsystemclient.Dataclass.CheckItemInfo;
import com.example.hospitalsystemclient.R;

import java.util.List;

public class CheckListAdapter extends RecyclerView.Adapter<CheckListAdapter.CheckItemViewHolder> {
    private List<CheckItemInfo> checkItemList;

    public CheckListAdapter(List<CheckItemInfo> checkItemList) {
        this.checkItemList = checkItemList;
    }

    public void update(List<CheckItemInfo> newCheckItemList) {
        this.checkItemList = newCheckItemList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CheckItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checklist, parent, false);
        return new CheckItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CheckItemViewHolder holder, int position) {
        CheckItemInfo checkItem = checkItemList.get(position);
        holder.tvCheckItemName.setText(checkItem.getCheckitemName());
        holder.tvCheckItemId.setText("编号: " + checkItem.getCheckitemId());
        holder.tvCheckItemPrice.setText("价格: " + String.format("%.2f", checkItem.getCheckitemPrice())+"元");
        holder.tvCheckPart.setText("科室: " + checkItem.getCheckPart());
        holder.tvCheckItemNote.setText("地址: " + checkItem.getCheckitemNote());
    }

    @Override
    public int getItemCount() {
        return checkItemList.size();
    }

    public static class CheckItemViewHolder extends RecyclerView.ViewHolder {
        public TextView tvCheckItemName;
        public TextView tvCheckItemId;
        public TextView tvCheckItemPrice;
        public TextView tvCheckPart;
        public TextView tvCheckItemNote;

        public CheckItemViewHolder(View itemView) {
            super(itemView);
            tvCheckItemName = itemView.findViewById(R.id.tvCheckItemName);
            tvCheckItemId = itemView.findViewById(R.id.tvCheckItemId);
            tvCheckItemPrice = itemView.findViewById(R.id.tvCheckItemPrice);
            tvCheckPart = itemView.findViewById(R.id.tvCheckPart);
            tvCheckItemNote = itemView.findViewById(R.id.tvCheckItemNote);
        }
    }
}